﻿using System;

namespace O1.Data.Traversal
{
    public class TraversalProjection : Projection
    {
        private readonly int initialCapacity;

        private PartitionedCellSet vertexResults;

        private CellAddress pendingVertex;

        public TraversalProjection(int initialCapacity)
            : base(PropertyInterests.None)
        {
            this.initialCapacity = initialCapacity;
        }

        public TraversalProjection(Filter filter, int initialCapacity)
            : base(filter)
        {
            this.initialCapacity = initialCapacity;
        }

        public bool HasVertices
        {
            get
            {
                return this.vertexResults != null;
                ////&& !this.vertexResults.IsEmpty;
            }
        }

        public PartitionedCellSet ResultVertices
        {
            get
            {
                if (this.vertexResults == null)
                {
                    this.vertexResults = new PartitionedCellSet(32 * this.initialCapacity);
                }

                return this.vertexResults;
            }
        }

        protected override void Project(Edge value, EdgeDirections direction)
        {
            this.pendingVertex = value;
        }

        protected override void ProjectEndEdge(ObservationInterests interest)
        {
            if (interest == ObservationInterests.Interested)
            {
                this.ResultVertices.Add(this.pendingVertex.CellId, this.pendingVertex.TrunkId);
            }
        }
    }
}
